package cool.zhoujie.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.List;

import cool.zhoujie.R;
import cool.zhoujie.gson.Data_QuerySong;

public class QueryAdapter extends ArrayAdapter<Data_QuerySong> {

    private int resourceId;

    public QueryAdapter(@NonNull Context context, int resource, @NonNull List<Data_QuerySong> objects) {
        super(context, resource, objects);
        this.resourceId = resource;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View view;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
        } else {
            view = convertView;
        }
        TextView idInfo = view.findViewById(R.id.search_id);
        TextView songName = view.findViewById(R.id.search_songName);
        TextView artistName = view.findViewById(R.id.search_artistName);
        Data_QuerySong data_querySong = getItem(position);
        String postionInfo = String.valueOf(position + 1);
        idInfo.setText(postionInfo);
        songName.setText(data_querySong.songName);
        artistName.setText(String.format("%s-%s", data_querySong.artistName, data_querySong.albumName));
        return view;
    }
}
